package com.project.quiktask.datastore;


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.Vector;

import com.project.quiktask.Task;
/**
 * A service class that handles file loading
 * @author PHAM MINH VIET
 */
public class FileLoader{
	private FileInputStream fstream;
	private BufferedReader reader;
	private DataInputStream inputStream;
	public FileLoader(String fileName) throws FileNotFoundException {
		fstream =new FileInputStream(fileName);
		inputStream = new DataInputStream(fstream);
		reader = new BufferedReader(new InputStreamReader(inputStream));
	}
	
	/**
	 * This method reads a list of Tasks from the file associated with inputFromFile
	 * @return List of Tasks that is stored in the file
	 * @throws IOException - occurs when access to file is failed
	 * @throws ParseException 
	 */
	public Vector<Task> load() throws IOException, ParseException {
		Vector<Task> TasksToBeLoaded = new Vector<Task>();
		Vector<String> inputContent=new Vector<String>();
		String stringLine;
		
		while((stringLine=reader.readLine())!=null ){
			inputContent.add(stringLine);
		}
		TasksToBeLoaded=FileConverter.generateTasks(inputContent);
		reader.close();
		return TasksToBeLoaded;
	}
	
	/**
	 * Closes the stream
	 * @throws IOException
	 */
	public void close() throws IOException {
		this.fstream.close();
	}
}
